/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.fd.manager.impl;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import com.xy.biz.fd.domain.po.DocumentHistoryPO;
import com.xy.tool.context.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.biz.fd.domain.bo.DocumentHistoryBO;
import com.xy.biz.fd.domain.query.DocumentHistoryQuery;
import com.xy.biz.fd.domain.converter.DocumentHistoryConverter;
import com.xy.biz.fd.manager.DocumentHistoryManager;
import com.xy.biz.fd.dao.DocumentHistoryDao;

/**
 *
 *
 * @author wcs
 * @date 2021-2-3
 */
@Service
public class DocumentHistoryManagerImpl implements DocumentHistoryManager {

	@Autowired
	private DocumentHistoryDao documentHistoryDao;

	@Override
	public int saveDocumentHistory(DocumentHistoryBO documentHistoryBO) {
		DocumentHistoryPO documentHistoryPO = DocumentHistoryConverter.toPO(documentHistoryBO);
		DocumentHistoryPO documentHistory = documentHistoryDao.getDocumentHistory(DocumentHistoryBO.builder()
				.documentId(documentHistoryPO.getDocumentId())
				.accessType(documentHistoryPO.getAccessType())
				.createUserId(UserContextHolder.getUserId()).build());
		if (Objects.isNull(documentHistory)) {
			return documentHistoryDao.saveDocumentHistory(documentHistoryPO);
		}
		documentHistory.setAccessTime(LocalDateTime.now());
		return documentHistoryDao.updateDocumentHistory(documentHistory);
	}

	@Override
	public int saveAllDocumentHistory(Collection<DocumentHistoryBO> collection) {
		return documentHistoryDao.saveAllDocumentHistory(DocumentHistoryConverter.toPO(collection));
	}

	@Override
	public int updateDocumentHistory(DocumentHistoryBO documentHistoryBO) {
		return documentHistoryDao.updateDocumentHistory(DocumentHistoryConverter.toPO(documentHistoryBO));
	}

	@Override
	public int updateDocumentHistoriesByIds(Collection<Long> ids) {
		return documentHistoryDao.updateDocumentHistoriesByIds(ids);
	}

	@Override
	public int deleteDocumentHistory(Long documentHistoryId) {
		return documentHistoryDao.deleteDocumentHistory(documentHistoryId);
	}

	/**
	 * 批量删除
	 *
	 * @param documentHistoryIdS
	 */
	@Override
	public int deleteDocumentHistories(Collection<Long> documentHistoryIdS) {
		return documentHistoryDao.deleteDocumentHistories(documentHistoryIdS);
	}

	/*@Override
	public DocumentHistoryBO getDocumentHistory(Long documentHistoryId) {
		return DocumentHistoryConverter.fromPO(documentHistoryDao.getDocumentHistory(documentHistoryId));
	}*/
	@Override
	public DocumentHistoryBO getDocumentHistory(DocumentHistoryBO documentHistoryBO) {
		return DocumentHistoryConverter.fromPO(documentHistoryDao.getDocumentHistory(documentHistoryBO));
	}

	@Override
	public List<DocumentHistoryBO> listDocumentHistoryById(Collection<Long> collection) {
		return DocumentHistoryConverter.fromPO(documentHistoryDao.listDocumentHistoryById(collection));
	}

	@Override
	public List<DocumentHistoryBO> listDocumentHistory(DocumentHistoryQuery query) {
		return DocumentHistoryConverter.fromPO(documentHistoryDao.listDocumentHistory(query));
	}

	@Override
	public int countDocumentHistory(DocumentHistoryQuery query) {
		return documentHistoryDao.countDocumentHistory(query);
	}

}
